﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AppMain
{
    public class ActionRole : BoxRole
    {
        public ActionRole(IAttrMap attrMap) : base(attrMap)
        {
        }


        private Queue<BaseAction> actionQueue = new Queue<BaseAction>();
        protected BaseAction currAction;
        public BaseAction CurrAction
        {
            get
            {
                return this.currAction;
            }
        }

        public void addAction(BaseAction baseAction)
        {
            if (IsFree)
            {
                if (this.currAction != null)
                {
                    this.currAction.exit();
                    currAction.dispose();
                    currAction = null;
                }
            }
            actionQueue.Enqueue(baseAction);
        }

        public override void tick(float frameTime)
        {
            base.tick(frameTime);
            if (currAction != null)
            {
                if (currAction.canRemove())
                {
                    currAction.exit();
                    currAction.dispose();
                    currAction = null;
                    if (actionQueue.Count > 0)
                    {
                        currAction = actionQueue.Dequeue();
                        currAction.enter(this);
                    }
                }
            }
            else
            {
                if (actionQueue.Count > 0)
                {
                    currAction = actionQueue.Dequeue();
                    currAction.enter(this);
                }
            }
            if (currAction != null)
                currAction.tick(frameTime);
        }

        public void say(string str)
        {
            BaseScene scene = this.AttrMap.getAttr<BaseScene>(RoleAttr.Scene).Value;
            if (scene != null)
            {
                pb.ChatRespMsg msg = new pb.ChatRespMsg();
                msg.content = str;
                msg.roleId = this.UID;
                msg.roleName = this.AttrMap.getAttr<string>(RoleAttr.NickName).Value;
                scene.broadCast(Cmd.ChatRespMsg, msg);
            }

        }

    }
}
